package chatclient.applets.note;




import chatclient.tools.NoteContainer;
import java.awt.*;

class TextPart implements NotePart {

    public TextPart(String s, boolean flag, Font font1, FontMetrics fontmetrics, Color color1) {
        text = s;
        eol = flag;
        font = font1;
        fm = fontmetrics;
        color = color1;
        whiteSpace = true;
        width = fontmetrics.stringWidth(s);
        for (int i = 0; i < s.length(); i++) {
            whiteSpace &= Character.isWhitespace(s.charAt(i));
        }

    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public boolean isWhiteSpace() {
        return whiteSpace;
    }

    @Override
    public boolean hasEol() {
        return eol;
    }

    @Override
    public NotePart breakAtWidth(int i, boolean flag, LinePart linepart) {
        int j;
        for (j = 0; j <= text.length(); j++) {
            if (fm.stringWidth(text.substring(0, j)) > i) {
                break;
            }
        }

        if (--j > 0 && j < text.length()) {
            int k = j;
            if (Character.isWhitespace(text.charAt(k))) {
                while (++k < text.length() && Character.isWhitespace(text.charAt(k))) {}
                if (k < text.length()) {
                    j = k;
                }
            } else {
                while (--k > 0 && !Character.isWhitespace(text.charAt(k - 1))) {}
                if (k > 0) {
                    j = k;
                }
            }
        }
        if (!flag && (j <= 0 || j >= text.length())) {
            return this;
        }
        if (j < 1) {
            j = text.length() > 0 ? 1 : 0;
        }
        if (j > 0) {
            int l;
            for (l = j; l > 0; l--) {
                if (!Character.isWhitespace(text.charAt(l - 1))) {
                    break;
                }
            }

            linepart.add(new TextPart(text.substring(0, l), false, font, fm, color), true);
        }
        if (j >= text.length()) {
            return null;
        } else {
            return new TextPart(text.substring(j), eol, font, fm, color);
        }
    }

    @Override
    public int getAscent() {
        return fm.getAscent();
    }

    @Override
    public int getDescent() {
        return fm.getDescent();
    }

    @Override
    public int getHeight() {
        return fm.getHeight();
    }

    @Override
    public int getLeading() {
        return fm.getLeading();
    }

    @Override
    public int getMaxAscent() {
        return fm.getMaxAscent();
    }

    @Override
    public int getMaxDescent() {
        return fm.getMaxDescent();
    }

    private double getColorDistance(Color color1, Color color2) {
        int i = color1.getRed() - color2.getRed();
        int j = color1.getGreen() - color2.getGreen();
        int k = color1.getBlue() - color2.getBlue();
        return Math.sqrt(i * i + j * j + k * k);
    }

    @Override
    public void paint(NoteContainer notecontainer, Note note, Graphics g, int i, int j, String s, Color color1,
            Double double1) {
        Color color2 = notecontainer.getNickColor(note.getNick(), s);
        if (color2 == null) {
            color2 = notecontainer.getChannelColor(note.getChannel(), s);
        }
        if (color2 == null) {
            color2 = color;
        }
        if (color2 == null || color2 != null && color1 != null && double1 != null && getColorDistance(color1, color2) < double1.doubleValue()) {
            color2 = notecontainer.getForeground(s);
        }
        g.setFont(font);
        g.setColor(color2);
        g.drawString(text, i, j + fm.getLeading() + fm.getAscent());
    }

    @Override
    public String toString() {
        return text;
    }
    
    public void setText(String value)
    {
        this.text = value;
    }
    
    private String text;
    private boolean eol;
    private boolean whiteSpace;
    private Font font;
    private FontMetrics fm;
    private Color color;
    private int width;
    

}
